/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.model.Construction;
import br.com.foxline.util.Caracter;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
public class ConstructionService extends GenericService<Construction> {

    public ConstructionService() {
        super(Construction.class);
    }
    
    @Override
    public void save(Construction entity){
        entity.setCnpj(Caracter.removecaracter(entity.getCnpj()));
        entity.setFone(Caracter.removecaracter(entity.getFone()));
        
        super.save(entity);
    }
    
    @Override
    public void update(Construction entity){
        entity.setCnpj(Caracter.removecaracter(entity.getCnpj()));
        entity.setFone(Caracter.removecaracter(entity.getFone()));
        
        super.update(entity);
    }

    public List<Construction> findConstruction(Construction construction) {
        List<Construction> customers = new ArrayList<Construction>();

        String sql = "SELECT c FROM Construction c WHERE ";

        if ((!construction.getName().equals("")) && (construction.getName() != null)) {
            sql += " lower(c.name) like lower(:name) and  ";
        }

        if ((!construction.getCnpj().equals("")) && (construction.getCnpj() != null)) {
            sql += " lower(c.cnpj) like lower(:cnpj) and  ";
        }

        sql += " c.active=TRUE";

        Query query = getEntityManager().createQuery(sql);

        if ((!construction.getName().equals("")) && (construction.getName() != null)) {
            query.setParameter("name", "%" + construction.getName() + "%");
        }
        if ((!construction.getCnpj().equals("")) && (construction.getCnpj() != null)) {
            query.setParameter("cnpj", "%" + construction.getCnpj() + "%");
        }

        customers = query.getResultList();
        return customers;
    }
}
